import com.ynny.chapter10.Bean.Student;
import com.ynny.chapter10.Mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentDaoTest {

    public void test() throws IOException {
        //1.读取核心配置文件
        InputStream resourceAsStream =
                Resources.getResourceAsStream("mybatis-config.xml");
        //2.sqlSession  工厂  ----》
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3.开启sql会话
        SqlSession sqlSession = sqlSessionFactory
                .openSession(true);
        StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);
        sqlSession = new SqlSessionFactoryBuilder().
                build(Resources.getResourceAsReader("mybatis-config.xml")).
                openSession(true);
        //根据账户更新密码
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        int s = studentMapper.updatePassword("刘备","66666" );
        System.out.println(s);

        //根据身份查询用户信息
        List<Student> students = studentMapper.selectByIdentity("帝王");
        for (Student student : students) {
            System.out.println(student);
        }


        //根据账户删除
        int affectedRows = studentMapper.deleteByAccount("刘备");
        System.out.println("受影响的行数: " + affectedRows);



    }
}

